<template>
	<view>
		<view v-if="webview_url != ''">
			<web-view :src="webview_url" @message="webviewMessage" @load="webviewLoad"></web-view>
		</view>
	</view>
</template>
 
<script>
	var app = getApp();
	var common = require('../../utils/common.js');
	var _this;

	export default {
		data() {
			return {
				webview_url: '',
				userInfo: '',
				title: '',
				shareResult: {},
				showShare: true,
				showWeb: false,
				urlList: [],
				needUpdate: false
			};
		},

		onShow() {
			if (_this.needUpdate) {
				_this.needUpdate = false;

				_this.webview_url = '';
				var url = _this.urlList[_this.urlList.length - 2];

				_this.urlList.splice(_this.urlList.length - 2, 2);

				_this.webview_url = url;
			}
		},

		onLoad: function(options) {
			_this = this;
			console.log(options);
			var webview_url = decodeURIComponent(options.webview_url);
			if(options.webview_title){
				var webview_title = decodeURIComponent(options.webview_title);
			}else{
				var webview_title = '';
			}
			
			console.log('999999999999999999999999999999999999999999888', typeof(webview_title));
			console.log('333333333333', webview_title, webview_url);

			if (webview_title && webview_title != '') {
				console.log('10000000000', webview_title);
				common.getTxtKey(webview_title).then(key => {
					uni.setNavigationBarTitle({
						title: key
					});
				}, () => {});
				_this.title = webview_title;
			} else {
				common.getTxtKey('打开页面').then(key => {
					uni.setNavigationBarTitle({
						title: key
					});
				}, () => {});
			}

			webview_url = webview_url.replace('http://', 'https://');

			if (webview_url.indexOf('https://') !== 0 && webview_url.indexOf('https%3A%2F%2F') === 0) {
				webview_url = decodeURIComponent(webview_url);
			}

			console.log('webview_url_', webview_url);
			var ticket = uni.getStorageSync('ticket');

			if (ticket) {
				if (app.globalData.scene == 1044 || app.globalData.scene == 1007) {
					uni.showToast({
						title: options.shareTickets,
						icon: 'success',
						duration: 5000
					});
					var obj = {
						'spread_openid': options.openId,
						'now_user_openid': app.globalData.userInfo.wxapp_openid,
						'code': wx.getStorageSync('code')
					};
					common.post('Home&a=wxapp_openid', obj, "scene", _this);
				}
			}

			_this.webview_url = webview_url;
			_this.userInfo = app.globalData.userInfo;

			common.setMainColor(_this, function(config) {
				_this.webviewLoad(webview_url);
			});
		},
		onShareAppMessage: function(options) {
			if (_this.shareResult.share_img && _this.shareResult.share_title) {
				let pageLink = _this.shareResult.share_url
				pageLink = common.addShareParam(pageLink)
				return {
					title: _this.shareResult.share_title,
					imageUrl: _this.shareResult.share_img,
					path: pageLink
				};
			} else if (_this.shareData) {
				let pageLink = '/pages/plat_menu/index?redirect=webview&webview_url=' + encodeURIComponent(_this.shareData.sendFriendLink) +
						"&webview_title=" + encodeURIComponent(_this.shareData.tTitle)
				pageLink = common.addShareParam(pageLink)
				return {
					title: _this.shareData.tTitle,
					path: pageLink
				};
			} else {
				let pageLink = _this.shareResult.share_url
				pageLink = common.addShareParam(pageLink)
				return {
					title: _this.title,
					path: pageLink
				};
			}
		},
		components: {},
		props: {},
		methods: {
			webviewLoad(e) {
				console.log('webview参数9999999999999999', e);

				if (typeof e == 'object') {
					var url = e.detail.src;

					_this.urlList.push(url);

					console.log('webview参数999999999999999988888888888', _this.urlList);
				} else {
					var url = e;
				}

				if (typeof e == 'object' && url == _this.webview_url) {
					return;
				} // 判断是否是订单详情页


				var pay_arr = app.globalData.config.pay_url_check_arr;

				for (var i = 0; i < pay_arr.length; i++) {
					if (url.indexOf(pay_arr[i]) != -1) {
						var order_from = wx.getStorageSync('order_from');

						if (order_from && order_from == 'pay') {
							wx.removeStorageSync('order_from');
							wx.redirectTo({
								url: '/pages/webview/webview?webview_url=' + encodeURIComponent(url) + '&webview_title=' + encodeURIComponent(
									'订单详情')
							});
						}
					}
				} //判断页面跳转
				//首页


				if (url.indexOf('c=Home&a=index') != -1 || common.strEndWith(url, '/wap.php')) {
					if (common.checkIsFooterPage("/pages/plat_menu/index")) {
						wx.switchTab({
							url: "/pages/plat_menu/index"
						});
					} else {
						_this.needUpdate = true;
						wx.navigateTo({
							url: '/pages/nofooter/index'
						});
					}

					return;
				} //我的


				if (url.indexOf('c=My&a=index') != -1) {
					console.log('my-yjy')
					if (common.checkIsFooterPage("/pages/plat_menu/my")) {
						wx.switchTab({
							url: "/pages/plat_menu/my"
						});
					} else {
						_this.needUpdate = true;
						wx.navigateTo({
							url: '/pages/nofooter/my_index'
						});
					}

					return;
				} //支付


				if (url.indexOf('c=Pay&a=check') != -1) {
					_this.needUpdate = true;
					var order_id = common.getUrlParam(url, 'order_id');
					var type = common.getUrlParam(url, 'type');
					wx.navigateTo({
						url: "/pages/shop_new/confirmOrder/pay?order_id=" + order_id + "&type=" + type
					});
					return;
				} //快店首页


				if (common.strEndWith(url, 'c=Shop&a=index') || common.strEndWith(url, 'c=Shop&a=classic_index')) {
					if (common.checkIsFooterPage("/pages/shop_index/index")) {
						wx.switchTab({
							url: "/pages/shop_index/index"
						});
					} else {
						_this.needUpdate = true;
						wx.navigateTo({
							url: '/pages/nofooter/shop/shop_index'
						});
					}

					return;
				} //请求分享参数


				console.log('config', _this.config);
				var postParam = {
					webview_url: url,
					noTip: true
				};

				if (_this.config && _this.config.city && _this.config.city.top_city && _this.config.city.top_city.area_id) {
					postParam['now_city'] = _this.config.city.top_city.area_id;
				}

				if (_this.config && _this.config.city && _this.config.city.top_city && _this.config.city.area_id != _this.config.city
					.top_city.area_id) {
					postParam['now_area'] = _this.config.city.area_id;
				}

				common.post('Wxapp&a=webview_share', postParam, function(result) {
					_this.shareResult = result;

					uni.setNavigationBarColor({
						frontColor: result.frontColor,
						backgroundColor: result.backgroundColor
					});

					if (result.shareMenu == true) {
						_this.showShare = true;
						//#ifdef MP-WEIXIN
						uni.showShareMenu({
							withShareTicket: true
						});
						//#endif
					} else {
						_this.showShare = false;
						uni.hideShareMenu();
					}

					console.log('333333333333333333333分享信息', _this.shareResult, _this.shareData);
				});
				_this.isFirstLoad = false;
			},

			webviewMessage: function(e) {
				var _this = this;

				console.log('e.detail.data', e.detail.data);
				var messageData = e.detail.data[e.detail.data.length - 1];

				if (messageData.page && messageData.page == 'changecity') {
					delete messageData.page;

					if (_this.webview_url.indexOf('from') != -1) {
						wx.setStorageSync('kuaidian_city', messageData);
					} else {
						wx.setStorageSync('now_city_info', messageData);
					}

					return false;
				}

				for (var i = e.detail.data.length - 1; i >= 0; i--) {
					var messageData = e.detail.data[i];
					console.log('messageData:' + i, messageData);

					if (messageData.msgType && messageData.msgType == 'share') {
						console.log('shareData', messageData);
						_this.shareData = messageData;
					}

					return false;
				}
			},

			getUserInfo(e) {
				if (e.detail.userInfo) {
					_this.webview_url = _this.webview_url;
					_this.userInfo = app.globalData.userInfo;
					_this.showWeb = true;

					if (app.globalData.scene == 1044 || app.globalData.scene == 1007) {
						wx.showToast({
							title: options.shareTickets,
							icon: 'success',
							duration: 5000
						});
						var obj = {
							'spread_openid': options.openId,
							'now_user_openid': userInfo.wxapp_openid,
							'code': wx.getStorageSync('code')
						};
						common.post('Home&a=wxapp_openid', obj, "scene", _this);
					}
				}
			},
			scene: function() {

			}
		}
	};
</script>
<style>
	/* pages/webview/webview.css */
</style>
